package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.apiResult.Result;
import com.ruoyi.common.apiResult.ZhtcRe;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.aikeService.ToZhtcApiService;
import com.ruoyi.system.apiQuery.AddOnlineMonthTicketTypeQuery;
import com.ruoyi.system.apiService.HomeParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkTicketTypeAddBo;
import com.ruoyi.system.bo.ZsyyParkTicketTypeQueryBo;
import com.ruoyi.system.bo.ZsyyParkTicketTypeEditBo;
import com.ruoyi.system.domain.ZsyyParkTicketType;
import com.ruoyi.system.mapper.ZsyyParkTicketTypeMapper;
import com.ruoyi.system.vo.ZsyyParkTicketTypeVo;
import com.ruoyi.system.service.IZsyyParkTicketTypeService;
import com.ruoyi.system.mapper.ZsyyParkTicketTypeMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * 月票类型Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkTicketTypeServiceImpl extends ServicePlusImpl<ZsyyParkTicketTypeMapper, ZsyyParkTicketType> implements IZsyyParkTicketTypeService {
    @Autowired
    private ZsyyParkTicketTypeMapper iZsyyParkTicketTypeMapper;
	@Resource
	private ToZhtcApiService toZhtcApiService;
	@Resource
	private HomeParkService homeParkService;

    @Override
    public ZsyyParkTicketTypeVo queryById(Long id){
        return getVoById(id, ZsyyParkTicketTypeVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkTicketTypeVo> queryPageList(ZsyyParkTicketTypeQueryBo bo) {
        PagePlus<ZsyyParkTicketType, ZsyyParkTicketTypeVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyParkTicketTypeVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkTicketTypeVo> queryList(ZsyyParkTicketTypeQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkTicketTypeVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkTicketType> buildQueryWrapper(ZsyyParkTicketTypeQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkTicketType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMonthTicketConfigId() != null, ZsyyParkTicketType::getMonthTicketConfigId, bo.getMonthTicketConfigId());
        lqw.eq(StrUtil.isNotBlank(bo.getParkCodes()), ZsyyParkTicketType::getParkCodes, bo.getParkCodes());
        lqw.like(StrUtil.isNotBlank(bo.getTicketName()), ZsyyParkTicketType::getTicketName, bo.getTicketName());
        lqw.eq(!CommonUtil.isEmpty(bo.getStatus()), ZsyyParkTicketType::getStatus, bo.getStatus());
        lqw.orderByDesc(ZsyyParkTicketType::getCreatetime);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkTicketTypeAddBo parkTicketType) {
		Map toTicket = new HashMap();
		toTicket.put("parkCodes",parkTicketType.getParkCodes());
		toTicket.put("ticketName",parkTicketType.getTicketName());
		toTicket.put("price",parkTicketType.getPrice());
		Map timePeriod = new HashMap();
		timePeriod.put("startTime", DateUtil.format(parkTicketType.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
		timePeriod.put("endTime",DateUtil.format(parkTicketType.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
		toTicket.put("timePeriod",timePeriod);
		String toTicketStr = JSON.toJSONString(toTicket);
		Map redata = null;
		if(parkTicketType.getParkCodes().indexOf("/")==-1){
			String data = toZhtcApiService.privateApi("addOnlineMonthTicketType ", toTicketStr,null);
			redata = JSON.parseObject(data,Map.class);
		}else{
			AddOnlineMonthTicketTypeQuery query = JSON.parseObject(toTicketStr, AddOnlineMonthTicketTypeQuery.class);
			ZhtcRe re = homeParkService.addOnlineMonthTicketType(query);
			redata = JSON.parseObject(JSON.toJSONString(re),Map.class);
		}
		if("业务成功".equals(redata.get("message"))){
			Map data1 = (Map)redata.get("data");
			parkTicketType.setMonthTicketConfigId(Long.parseLong(data1.get("monthTicketConfigId").toString()));
			parkTicketType.setStatus(1);    //默认为上架
			ZsyyParkTicketType add = BeanUtil.toBean(parkTicketType, ZsyyParkTicketType.class);
			validEntityBeforeSave(add);
			return save(add);
		}else {
			return false;
		}
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkTicketTypeEditBo bo) {
        ZsyyParkTicketType update = BeanUtil.toBean(bo, ZsyyParkTicketType.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkTicketType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
